//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "aura.h"
#include "private.h"
#include <errno.h>
#include <stdio.h>
#include <sys/mman.h>

void _assert(const char *exp, const char *f, int ln)
{
    Aura_printf("Assertion failed: %s, file %s, line %d\n", exp, f, ln);
    return ;
}

Aura_ECode GetLastAuraECode()
{
    Aura_ECode ec = AURA_NOERROR;

    switch(errno) {
    case ENOMEM:
        ec = AURA_E_OUT_OF_MEMORY;
        break;
    case EINTR:
        ec = AURA_E_INTERRUPTED;
        break;

    case EPERM:
    case ESRCH:
    default:
        Aura_printf(" (Linux) Last Error: %d.\n", errno);
        perror(" linux fail: ");
        ec = AURA_E_FAIL;
    }

    return ec;
}

int InfiniteFilter(Aura_Millisecond32 timeOut)
{
    int mseconds = 0;
    if (timeOut < 0) {
        mseconds = 0x7FFFFFFF;
    }
    else {
        mseconds = timeOut;
    }

    return mseconds;
}

int AuraMemoryMapFlagsConvertToLinux(Aura_MemoryMapFlags flags)
{
    int lxFlags = 0;

    lxFlags |= MAP_PRIVATE;

    if(flags & Aura_MemoryMap_Private){
        lxFlags |= MAP_PRIVATE;
    }
    if(flags & Aura_MemoryMap_Shared){
        lxFlags |= MAP_SHARED;
    }
    if(flags & Aura_MemoryMap_Fixed){
        lxFlags |= MAP_FIXED;
    }
    if(flags & Aura_MemoryMap_Downgrow){
        lxFlags |= MAP_GROWSDOWN;
    }

    lxFlags |= MAP_ANONYMOUS;

    return lxFlags;
}

int AuraMemoryProtectionConvertToLinux(Aura_MemoryProtection protect)
{
    int lxProtect;

    switch(protect)
    {
        case Aura_MemoryProtection_Read:
            lxProtect = PROT_READ;
            break;

        case Aura_MemoryProtection_Write:
            lxProtect = PROT_WRITE;
            break;

        case Aura_MemoryProtection_Exec:
            lxProtect = PROT_EXEC;
            break;

        case Aura_MemoryProtection_RW:
        case Aura_MemoryProtection_RWIO:
            lxProtect = PROT_READ|PROT_WRITE;
            break;

        case Aura_MemoryProtection_RX:
            lxProtect = PROT_READ|PROT_EXEC;
            break;

        case Aura_MemoryProtection_RWX:
            lxProtect = PROT_READ|PROT_EXEC|PROT_EXEC;
            break;

        case Aura_MemoryProtection_IO:
        default:
            lxProtect = PROT_READ;
            break;
    }

    return lxProtect;
}


Aura_ThreadPriority ConvertToAuraPriority(
    /* [in]  */ int priority)
{
    Aura_ThreadPriority pri;

    if(priority < -15) {
        pri = Aura_ThreadPriority_Highest;
    }
    else if(priority < -5) {
         pri = Aura_ThreadPriority_AboveNormal;
    }
    else if(priority < 5) {
        pri = Aura_ThreadPriority_Normal;
    }
    else if(priority < 15) {
        pri = Aura_ThreadPriority_BelowNormal;
    }
    else {
        pri = Aura_ThreadPriority_Lowest;
    }

    return pri;
}

int ConvertToLinuxPriority(
    /* [in]  */ Aura_ThreadPriority priority)
{
    int pri = 0;

    switch (priority)
    {
        case Aura_ThreadPriority_Lowest:
            pri = 20;
            break;
        case Aura_ThreadPriority_BelowNormal:
            pri = 10;
            break;
        case Aura_ThreadPriority_Normal:
            pri = 0;
            break;
        case Aura_ThreadPriority_AboveNormal:
            pri = -10;
            break;
        case Aura_ThreadPriority_Highest:
            pri = -20;
            break;
        default:
            assert(0); // should never happen.
    }

    return pri;
}
